package com.mbc.web.common.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;

public class MenubarTag extends Composite {

	private static MenubarTagUiBinder uiBinder = GWT
			.create(MenubarTagUiBinder.class);

	interface MenubarTagUiBinder extends UiBinder<Widget, MenubarTag> {
	}

	public MenubarTag() {
		initWidget(uiBinder.createAndBindUi(this));
		this.getElement().setId("menubar");
		initialize();
	}
	
	private void initialize()
	{
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				toggleSidebar();
				
				Element l_ele = DOM.getElementById("aLogout");
				Event.sinkEvents(l_ele, Event.ONCLICK);
				Event.setEventListener(l_ele, new EventListener() {
					
					@Override
					public void onBrowserEvent(Event event) {
						if(event.getTypeInt() == Event.ONCLICK){
							CommonControllerMgr.logout(logoutCallback());
						}
					}
				});
				
				Element l_resetPwd = DOM.getElementById("aResetPwd");
				Event.sinkEvents(l_resetPwd, Event.ONCLICK);
				Event.setEventListener(l_resetPwd, new EventListener() {
					
					@Override
					public void onBrowserEvent(Event event) {
						if(event.getTypeInt() == Event.ONCLICK)
							ClientSingleton.changeView(new FrmResetPassword());
					}
				});
			}
		});
	}
	
	private AsyncCallback<Integer> logoutCallback(){
		return new AsyncCallback<Integer>() {
			
			@Override
			public void onSuccess(Integer result) {
				ClientSingleton.showLogin();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}
	
	private native void toggleSidebar()/*-{
		$wnd.$("#a-lg-toggle").click(function(){				
			if($wnd.$("body").hasClass("body-sidebar-hide"))
				$wnd.$("body").removeClass("body-sidebar-hide");
			else
				$wnd.$("body").addClass("body-sidebar-hide");
				
			var l_parentWidht = $wnd.$('.table-scroll').parent().width();
	    	$wnd.$('.table-scroll').width(l_parentWidht).perfectScrollbar('update');
		});
		
		$wnd.$("#a-sm-toggle").click(function(){
			
			if($wnd.$("#sidebar").hasClass("sidebar-sm"))
				$wnd.$("#sidebar").attr("class","sidebar");
			else
				$wnd.$("#sidebar").attr("class","sidebar-sm");
			var l_parentWidht = $wnd.$('.table-scroll').parent().width();
	    	$wnd.$('.table-scroll').width(l_parentWidht).perfectScrollbar('update');
		});
	}-*/;
}
